/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.wtw.konnekt;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Adam
 */
public class Message {
    //private String[] MY_UUID = {"263433","markowski.adam@gmail.com"};
    private String[] MY_UUID;
    public static int MSG_FLAG_OUTGOING = 6;
    public static int MSG_FLAG_INCOMMING = 5;
    private static int MSG_NET_GG = 10;
    private static int MSG_NET_XMPP = 12;
    private long chatId;
    private String contactId;
    private String contactNet;
    private long entryTime;
    private String entryText;
    private int entryFlags;
//    private int flags;

    private String author;


    public Message(String contactName, String author, String sDate, String sTime, String sNet, String sType, String sIdFrom, String sIdTo, String sMsg, String[] myUid) throws ParseException {
        this.MY_UUID = myUid;
        if (sIdFrom == null) sIdFrom = "";
        if (sIdTo == null) sIdTo = "";
        if (author == null) author = "";
        if (sDate == null) sDate = "";
        if (sTime == null) sTime = "";
        if (sNet == null) sNet = "";
        if (sType == null) sType = "";

        DateFormat df = new SimpleDateFormat("dd-MM-yyyy kk:mm:ss");
        Date date;
        try {
            date = df.parse(sDate+" "+sTime);
        } catch (ParseException ex) {
            df = new SimpleDateFormat("dd-MM-yyyy kk:mm");
            date = df.parse(sDate+" "+sTime);
        }
        this.entryTime = date.getTime()/1000;
        int netId = Integer.parseInt(sNet);
        if (netId == MSG_NET_GG) {
            this.contactNet = "GG";
        } else if (netId == MSG_NET_XMPP) {
            this.contactNet = "XMPP";
        }
        else {
            throw new UnsupportedOperationException("Not yet implemented net id");
        }

        for (int i=0;i<MY_UUID.length;i++) {
            if (MY_UUID[i].matches(sIdFrom)) {
                //kontakt jest autorem
                this.entryFlags = MSG_FLAG_OUTGOING;
                if (sIdTo.length()>0) {
                    this.contactId = sIdTo;
                } else {
                    this.contactId = sIdFrom;
                }
            }else if (MY_UUID[i].matches(sIdTo)) {
                //kontakt jest autorem
                this.entryFlags = MSG_FLAG_INCOMMING;
                if (sIdFrom.length()>0) {
                    this.contactId = sIdFrom;
                } else {
                   this.contactId = sIdTo;
                }
            }
        }
        if (contactId==null) {
            //ja jestem autorem, przypisac kontakt
            if (sIdFrom!= null && sIdFrom.length()!=0){
                this.contactId = sIdFrom;
                this.entryFlags = MSG_FLAG_INCOMMING;
            } else if (sIdTo!=null && sIdTo.length()!=0){
                this.contactId = sIdTo;
                this.entryFlags = MSG_FLAG_OUTGOING;
            } else {
                throw new ParseException("kupa z id...", 0);
            }
        }
        this.entryText = sMsg;
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("\nMessage\n");
        sb.append("\n");
        sb.append(" contactId: "+contactId);
        sb.append("\n");
        sb.append(" contactNet: "+contactNet);
        sb.append("\n");
        sb.append(" entryTime: "+DateFormat.getInstance().format(new Date(entryTime*1000)));
        sb.append("\n");
        sb.append(" entryText: "+entryText);
        sb.append("\n");
        sb.append(" entryFlags: "+entryFlags);

        return sb.toString();
    }

    /**
     * @return the chatId
     */
    public long getChatId() {
        return chatId;
    }

    /**
     * @param chatId the chatId to set
     */
    public void setChatId(long chatId) {
        this.chatId = chatId;
    }

    /**
     * @return the contactId
     */
    public String getContactId() {
        return contactId;
    }

    /**
     * @param contactId the contactId to set
     */
    public void setContactId(String contactId) {
        this.contactId = contactId;
    }

    /**
     * @return the contactNet
     */
    public String getContactNet() {
        return contactNet;
    }

    /**
     * @param contactNet the contactNet to set
     */
    public void setContactNet(String contactNet) {
        this.contactNet = contactNet;
    }

    /**
     * @return the entryTime
     */
    public long getEntryTime() {
        return entryTime;
    }

    /**
     * @param entryTime the entryTime to set
     */
    public void setEntryTime(long entryTime) {
        this.entryTime = entryTime;
    }

    /**
     * @return the entryText
     */
    public String getEntryText() {
        return entryText;
    }

    /**
     * @param entryText the entryText to set
     */
    public void setEntryText(String entryText) {
        this.entryText = entryText;
    }

    /**
     * @return the entryFlags
     */
    public int getEntryFlags() {
        return entryFlags;
    }

    /**
     * @param entryFlags the entryFlags to set
     */
    public void setEntryFlags(int entryFlags) {
        this.entryFlags = entryFlags;
    }

    /**
     * @return the MY_UUID
     */
    public String[] getMY_UUID() {
        return MY_UUID;
    }

    /**
     * @param MY_UUID the MY_UUID to set
     */
    public void setMY_UUID(String[] MY_UUID) {
        this.MY_UUID = MY_UUID;
    }

//    /**
//     * @return the flags
//     */
//    public int getFlags() {
//        return flags;
//    }
//
//    /**
//     * @param flags the flags to set
//     */
//    public void setFlags(int flags) {
//        this.flags = flags;
//    }


}
